package com.ccy.utils;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;

public final class IoUtils {

	/** {@value} */
	public static final int DEFAULT_BUFFER_SIZE = 32 * 1024; // aka(also known as) 32 KB

	private IoUtils() {
	}

	/**
	 * Read all data from stream and close it silently 
	 */
	public static void readAndCloseStream(InputStream is) {
		final byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
		try {
			while (is.read(bytes, 0, DEFAULT_BUFFER_SIZE) != -1) {
			}
		} catch (IOException e) { // ignore
		} finally {
			closeSilently(is);
		}
	}

	/**
	 * close closeable object (inputstream outputstream etc.) without exception
	 */
	public static void closeSilently(Closeable closeable) {
		if (closeable != null) {
			try {
				closeable.close();
				closeable = null;
			} catch (IOException e) {// ignore
			}
		}
	}
}
